//** Craig Laurent
#import <sys/param.h>		//** for MAXHOSTNAMELEN constant
#import <sys/types.h>		//** for getpwnam()
#import <pwd.h>				//** for struct passwd
#import "User.h"

@implementation User

- init
{
	if (self = [super init]) {
		userID = nil;
		userName = nil;
		userMachine = nil;
		return self;
	}
	return nil;
}

- (void)dealloc
{
	if ([self userID])
		[userID autorelease];
	if ([self userName])
		[userName autorelease];
	if ([self userMachine])
		[userMachine autorelease];
	[super dealloc];
}

//****************************************
//** instance methods
- (NSString *)userID
{	return userID; }
- (void)setUserID:(NSString *)newID
{
	[userID autorelease];
	userID = [newID retain];
}

- (NSString *)userName
{	return userName; }
- (void)setUserName:(NSString *)newName
{
	[userName autorelease];
	userName = [newName retain];
}

- (NSString *)userMachine
{	return userMachine; }
- (void)setUserMachine:(NSString *)newMachine
{
	[userMachine autorelease];
	userMachine = [newMachine retain];
}


//****************************************
//** method types
/* loadUser - load User information from Unix system */
- (void)loadUser
{
	char tempName[MAXHOSTNAMELEN];
	struct passwd *passwordInfo;

	//** get User Information from Unix
	passwordInfo = getpwuid(getuid());
	[self setUserID:[NSString stringWithCString:passwordInfo->pw_name]];
	[self setUserName:[NSString stringWithCString:passwordInfo->pw_gecos]];

	//** get Machine Name from Unix
	gethostname(tempName, MAXHOSTNAMELEN);
	[self setUserMachine:[NSString stringWithCString:tempName]];
}

/* userNicknameWithID:... - returns a string with the pieces of user information that this user wants represented. */
- (NSString *)userNicknameWithID:(BOOL)ID andName:(BOOL)name andMachine:(BOOL)machine
{
	NSMutableString *nickname;
	int arguments = 0;

	nickname = [[[NSMutableString alloc] init] autorelease];

	//** create nickname
	//** add user's ID first
	if (ID && [self userID]) {
		[nickname appendString:[self userID]];
		arguments++;
	}

	//** add user's machine name (after an "at" sign)
	if (machine && [self userMachine]) {
		[nickname appendString:@"@"];
		[nickname appendString:[self userMachine]];
		arguments++;
	}

	//** add user's Real Name  last
	//**  use parenthesis if not the only argument in the nickname
	if (name && [self userName]) {
		if (arguments > 0) {
			[nickname appendString:@" ("];
			[nickname appendString:[self userName]];
			[nickname appendString:@")"];
		} else
			[nickname appendString:[self userName]];
	}

	return nickname;
}


//****************************************
//** data archiving methods
/* encodeWithCoder: - encode User info for writing to file. */
- (void)encodeWithCoder:(NSCoder *)aCoder
{
	[super encodeWithCoder:aCoder];
	[aCoder encodePropertyList:[self userID]];
	[aCoder encodePropertyList:[self userName]];
	[aCoder encodePropertyList:[self userMachine]];
}

/* initWithCoder: - decode User info after reading from file. */
- initWithCoder:(NSCoder *)aDecoder
{
	[super initWithCoder:aDecoder];
	[self setUserID:[aDecoder decodePropertyList]];
	[self setUserName:[aDecoder decodePropertyList]];
	[self setUserMachine:[aDecoder decodePropertyList]];

    return self;
}


//****************************************
//** Distributed Object encoding methods
/* encodeRemotelyFor:.. -  Distributed Object method to indicate what should be passed, The object or a copy.  NSObject won't be passed as a copy. */
- encodeRemotelyFor:(NXConnection *)connection freeAfterEncoding:(BOOL *)flagp isBycopy:(BOOL)isBycopy
{
	if (isBycopy)
		return self;	//** return copy of object, not a proxy
//	return nil;	//** NSObject doesn't encode for DO.
	return [super encodeRemotelyFor:connection freeAfterEncoding:flagp isBycopy:isBycopy];
}

/* encodeUsing: -  Distributed Object method to encode the data for transfer */
- encodeUsing:(id <NXEncoding>)portal
{
	[portal encodeData:&userID ofType:"@"];
	[portal encodeData:&userName ofType:"@"];
	[portal encodeData:&userMachine ofType:"@"];
	return self;
}

/* decodeUsing: -  Distributed Object method to decode the data after transfer */
- decodeUsing:(id <NXDecoding>)portal
{
	self = [[self init] autorelease];

	//** decode data
	[portal decodeData:&userID ofType:"@"];
	[portal decodeData:&userName ofType:"@"];
	[portal decodeData:&userMachine ofType:"@"];

	//** retain data
	[userID retain];
	[userName retain];
	[userMachine retain];

	return self;
}


@end
